package cate.game.stat.shop;

import cate.game.GameBody;
import cate.game.res.MixRes;
import cate.game.res.MixResIo;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cp.solution.business.stat.item.ShopBuyLog;
import cp.solution.rpc.path.RpcStatPath;
import easy.java.time.DateTool;
import lombok.Setter;
import lombok.experimental.Accessors;

public class ShopBuyAction {


    private GameBody game;

    private int accountId;

    private String roleId;

    private String roleName;

    private int roleLevel;

    private int roleVip;

    private int srvId;

    private String groupCode;

    private String gameId;

    @Setter @Accessors(fluent = true)
    private int mallId;

    @Setter @Accessors(fluent = true)
    private int mallItemId;

    @Setter @Accessors(fluent = true)
    private int mallItemNum;

    private byte resInType;

    private int resInTid;

    private long resInNum;

    private byte resOutType;

    private int resOutTid;

    private long resOutNum;

    private long time;

    @Override
    public String toString() {
        return "账号ID="+accountId+" 角色："+roleName+"("+roleId+") lv" + roleLevel + " vip" + roleVip + " srvId=" + srvId
                + "(" + groupCode + ") GameID=" + gameId + " 商店ID=" + mallId + " 商品=" + mallItemId + " × " + mallItemNum
                + " 消耗=" + resInType + ":" + resInTid+ ":" + resInNum
                + " 获得=" + resOutType + ":" + resOutTid+ ":" + resOutNum
                + " 时间=" + DateTool.instance().format(time);
    }

    public void echo() {
        System.out.println(this);
    }

    public static ShopBuyAction build(Role role) {
        ShopBuyAction action = new ShopBuyAction();
        action.game = role.getGame();
        action.time = action.game.time.now();
        action.accountId = role.getBase().accountId;
        action.roleId = role.getUid();
        action.roleName = role.getBase().name;
        action.roleLevel = role.getBase().level;
        action.roleVip = role.getBase().vip;
        action.srvId = action.game.config.srvId;
        action.groupCode = action.game.config.gs.getGroupCode();
        action.gameId = role.getBase().gameId;
        return action;
    }

    /** 如果没有MixResIo，你可以分开来设置consume和reward */
    public ShopBuyAction io(MixResIo resIo) {
        return consume(resIo.in()).reward(resIo.out());
    }

    public ShopBuyAction consume(MixRes _in) {
        if(_in != null && _in.items != null && _in.items.size() > 0) {
            MixResItem item = _in.items.get(0);
            this.resInType = item.type;
            this.resInTid = item.tid();
            this.resInNum = item.num;
        }
        return this;
    }

    public ShopBuyAction reward(MixRes _out) {
        if(_out != null && _out.items != null && _out.items.size() > 0) {
            MixResItem item = _out.items.get(0);
            this.resOutType = item.type;
            this.resOutTid = item.tid();
            this.resOutNum = item.num;
        }
        return this;
    }

    public void send() {
        game.rpcSend().fireAndForgetStat(RpcStatPath.SHOP_BUY_ACTION, toEntity());
    }

    private ShopBuyLog toEntity() {
        ShopBuyLog entity = new ShopBuyLog();
        entity.setAccountId(accountId);
        entity.setRoleId(roleId);
        entity.setRoleName(roleName);
        entity.setRoleLevel(roleLevel);
        entity.setRoleVip(roleVip);
        entity.setSrvId(srvId);
        entity.setGroupCode(groupCode);
        entity.setGameId(gameId);
        entity.setMallId(mallId);
        entity.setMallItemId(mallItemId);
        entity.setMallItemNum(mallItemNum);
        entity.setResInType(resInType);
        entity.setResInTid(resInTid);
        entity.setResInNum(resInNum);
        entity.setResOutType(resOutType);
        entity.setResOutTid(resOutTid);
        entity.setResOutNum(resOutNum);
        entity.setBuyTime(time);
        return entity;
    }
}
